package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.ApplyCityHistoryBean;
import com.shenzhen.horselive.beans.ApplyHistory;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.databinding.ActivityApplyCityDetailsBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.ui.dialog.CommentDialog;
import com.shenzhen.horselive.ui.dialog.InputDialog;
import com.shenzhen.horselive.utils.A;
import com.shenzhen.horselive.utils.DialogManager;
import com.shenzhen.horselive.widget.VerificationCodeInput;

public class ApplyCityDetailsActivity extends AppBarActivity {

    ActivityApplyCityDetailsBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityApplyCityDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleRed();
        setTitle("城市合伙人");
        initViews();
    }

 ApplyCityHistoryBean.DataBean  dataBean;
    private void initViews() {
       dataBean = (ApplyCityHistoryBean.DataBean) getIntent().getSerializableExtra("data");
        initType(dataBean.getStatus());

    }

    //区分状态
    private void initType(int type) {
//        申请状态：0：提交待审核；3:审核待支付；
//        2审核拒绝；1:已开通；4:竞拍中；5：已售出；6：已回收  7是过期未审核已经取消
        switch (type)
        {

            case 0:
                binding.imgType.setImageResource(R.mipmap.ico_daishenghe);
                binding.tvType.setText("待审核");
                binding.tvContent.setText("您的城市合伙人申请已提交，等待平台审核中");
                binding.btGo.setVisibility(View.GONE);
                //显示时间
                binding.linerTime.setVisibility(View.VISIBLE);
                binding.tvTextTime.setText("提交时间");
                binding.tvTime.setText(""+dataBean.getCreateTime());
                break;
            case 1:
                //已经开通
                setMenuText("收益明细");
                binding.imgType.setImageResource(R.mipmap.ico_has_kaitong);
                binding.tvType.setText("已开通");
                binding.tvContent.setText("您申请的城市合伙人已开通");
                //开通日期
                binding.linerTime.setVisibility(View.VISIBLE);
                binding.tvTextTime.setText("开通日期");
                binding.tvTime.setText(""+dataBean.getUpdateTime());
                //有效期
                binding.linerEndTime.setVisibility(View.VISIBLE);
                binding.tvTextEndtime.setText("开通日期");
                if ("-1".equals(dataBean.getEffectiveTime()))
                {
                binding.tvEndtime.setText("永久");
                }
                else
                {
                    binding.tvEndtime.setText(dataBean.getEffectiveTime()+"天");
                }
                //当前收益
                binding.linerProfit.setVisibility(View.VISIBLE);
                binding.tvTextProfit.setText("当前总收益");
                binding.tvProfit.setText(dataBean.getTotalProfit()+"(蜂值)");
                binding.btGo.setText("出售竞拍");
                binding.btGo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        DialogManager.getInstance().showCommentDialog(getContext(), "确认出售",
                                String.format("当前城市将以¥%s的价格\n进行拍卖，一旦确认出售将不可撤回", dataBean.getAmount()),
                                new CommentDialog.OnClickButton() {
                                    @Override
                                    public void confirm() {
                                        //调用拍卖的接口
                                        DialogManager.getInstance().showInputDialog(getContext(), "确认出售"
                                , String.format("当前城市将以¥%s的价格\n进行拍卖，一旦确认出售将不可撤回", dataBean.getAmount()),
                                new InputDialog.OnInputFinsh() {
                                    @Override
                                    public void onFinish(String s) {
                                        //调用拍卖支付的接口
                                     ApiFactory.getApiService().saleCityPartner(User.get().getMyInfo().getToken()
                                     ,dataBean.getOrderCode(),s)
                                             .compose(SchedulersCompat.applyIoSchedulers())
                                             .safeSubscribe(new LoadingViewObserver<CommResponseBean>(ApplyCityDetailsActivity.this) {
                                                 @Override
                                                 protected void onNextInActive(CommResponseBean commResponseBean) {
                                                     if (commResponseBean.isSuccess())
                                                     {
                                                        showToast("出售成功");
                                                        finish();
                                                     }
                                                     else
                                                     {
                                                         showToast(commResponseBean.getErrMessage());
                                                     }
                                                 }
                                             });
                                    }
                                }
                        );
                                    }

                                    @Override
                                    public void cancel() {

                                    }
                                }
                        );

                    }
                });

                break;
            case 2:
                //审核拒绝
                binding.imgType.setImageResource(R.mipmap.ico_yijujue);
                binding.tvType.setText("已拒绝");
                binding.tvContent.setText("您的城市合伙人申请已拒绝\n" +
                        "拒绝原因：该城市已由他人申请成功");
                binding.btGo.setVisibility(View.GONE);
                //拒绝时间
                binding.linerTime.setVisibility(View.VISIBLE);
                binding.tvTextTime.setText("拒绝时间");
                binding.tvTime.setText(""+dataBean.getUpdateTime());
                break;
            case 3:
                //审核待支付
                binding.imgType.setImageResource(R.mipmap.ico_applycity_needpay);
                binding.tvType.setText("待支付");
                //剩余支付时间倒计时
                binding.tvContent.setText(String.format("您的申请已通过，请尽快支付\n" +
                        "支付截止时间%s\n" +
                        "过期未支付，本次申请将自动取消",dataBean.getEndTime()+""));
                binding.btGo.setText("立即支付");
                binding.btGo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //先获取峰值剩余价值
                        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),1+"")
                                .compose(SchedulersCompat.applyIoSchedulers())
                                .safeSubscribe(new LoadingViewObserver<UserAcountBean>(ApplyCityDetailsActivity.this) {
                                    @Override
                                    protected void onNextInActive(UserAcountBean userAcountBean) {
                                        if (userAcountBean.isSuccess())
                                        {
                                            showPayDialog(userAcountBean);
                                        }else
                                        {
                                            showToast(userAcountBean.getErrMessage());
                                        }
                                    }
                                });
                    }
                });
                break;
            case 4:
//                竞拍中
                binding.tvType.setText("竞拍中");
                binding.tvContent.setVisibility(View.GONE);
                binding.btGo.setVisibility(View.GONE);
                break;
            case 5:
                // 已售出
                binding.tvType.setText("已售出");
                binding.tvContent.setVisibility(View.GONE);
                binding.btGo.setVisibility(View.GONE);
                break;
            case 6:
                // 已回收
                binding.tvType.setText("已回收");
                binding.tvContent.setVisibility(View.GONE);
                binding.btGo.setVisibility(View.GONE);
                break;
            default:
                //其他默认状态
                binding.imgType.setImageResource(R.mipmap.ico_yiquxiao);
                binding.tvType.setText("已取消");
                binding.tvContent.setText("您的城市合伙人申请已取消");
                //取消时间
                binding.linerTime.setVisibility(View.VISIBLE);
                binding.tvTextTime.setText("取消时间");
                binding.tvTime.setText(""+dataBean.getUpdateTime());
                binding.btGo.setVisibility(View.GONE);
                break;
        }
      //共有信息
        binding.tvName.setText(dataBean.getApplicationName());
        binding.tvPhone.setText(dataBean.getApplicationPhone());
        binding.tvCity.setText(dataBean.getCity()+"-"+dataBean.getDistrict());
        binding.tvPrice.setText(dataBean.getAmount()+"（蜂值）");
    }
    //展示支付弹窗
    private void showPayDialog(UserAcountBean userAcountBean) {
        //峰值兑换人命币比率
        double  radio = Double.parseDouble(userAcountBean.getData().getRatio());
        CommentDialog  commentDialog = new CommentDialog(ApplyCityDetailsActivity.this);
        commentDialog.setTitleText("支付");
        commentDialog.setContent("选择城市价格：" +dataBean.getAmount()+"蜂值"+
                "\n账户余额："+ formatFuckingNumber(userAcountBean.getData().getAmount())+"蜂值");
        commentDialog.setBtText("取消","确认支付");
        //取消
        commentDialog.setCancel(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commentDialog.dismiss();
            }
        });
        //支付
        commentDialog.setConfirm(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commentDialog.dismiss();
                InputDialog  inputDialog = new InputDialog(ApplyCityDetailsActivity.this);
                inputDialog.setTextName("支付");
                inputDialog.setTextContent(String.format("支付价格：¥%s",dataBean.getAmount()));
                inputDialog.setInputDown(new VerificationCodeInput.Listener() {
                    @Override
                    public void onComplete(String content) {
                        inputDialog.dismiss();
                       //调用支付接口
                      ApiFactory.getApiService().payCityPartner(User.get().getMyInfo().getToken(),dataBean.getOrderCode(),
                              content
                              ).compose(SchedulersCompat.applyIoSchedulers())
                              .safeSubscribe(new LoadingViewObserver<CommResponseBean>(ApplyCityDetailsActivity.this) {
                                  @Override
                                  protected void onNextInActive(CommResponseBean commResponseBean) {
                                      if (commResponseBean.isSuccess())
                                      {
                                          //
                                          showToast("支付成功");
                                          finish();
                                      }
                                      else {
                                          //支付失败
                                          showToast(commResponseBean.getErrMessage());
                                          binding.tvType.setText("支付失败");
                                          binding.btGo.setText("重新支付");
                                      }
                                  }
                              });

                    }
                });
                inputDialog.show();
            }
        });
        commentDialog.show();
    }
    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();

    }
    public static void start(Context context, ApplyCityHistoryBean.DataBean data) {
        Intent starter = new Intent(context, ApplyCityDetailsActivity.class);
        starter.putExtra("data",data);
        context.startActivity(starter);
    }
}